package com.lt.bcra.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lt.bcra.entity.*;
import com.lt.bcra.entity.vo.QuestionnaireInfoVo;
import com.lt.bcra.mapper.BcraQuestionnaireResponseMapper;
import com.lt.bcra.entity.*;
import com.lt.bcra.exception.BaseException;
import com.lt.bcra.service.*;
import com.lt.bcra.result.ResponseEnum;
import com.lt.bcra.result.ResultData;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lt.bcra.service.*;
import lt.bcra.handler.QuestionnaireCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 回收问卷表 服务实现类
 * </p>
 *
 * @author LuoTian
 * @since 2022-05-25
 */
@Service
//事务注解
@Transactional
public class BcraQuestionnaireResponseServiceImpl extends ServiceImpl<BcraQuestionnaireResponseMapper, BcraQuestionnaireResponse> implements BcraQuestionnaireResponseService {

    @Autowired
    BcraQuestionnaireResponseMapper bcraQuestionnaireResponseMapper;

    @Autowired
    BcraPersonalFactorsService bcraPersonalFactorsService;

    @Autowired
    BcraPhysiologicalFactorsService bcraPhysiologicalFactorsService;

    @Autowired
    BcraLifeHabitsService bcraLifeHabitsService;

    @Autowired
    BcraHealthInformationService bcraHealthInformationService;

    @Autowired
    BcraFamilyHistoryService bcraFamilyHistoryService;

    @Autowired
    BcraGeneTestService bcraGeneTestService;

    @Override
    public ResultData createNewQues(QuestionnaireInfoVo questionnaireInfoVo, String phone, String name) {

        //生成问卷编号
        String questionnaireCode=getQuestionnaireCode(phone);

        Map<String, Object> map = new HashMap<>();

        //个人因素表填写
        BcraPersonalFactors bcraPersonalFactors = questionnaireInfoVo.getBcraPersonalFactors();
        bcraPersonalFactors.setQuestionnaireCode(questionnaireCode);
        ResultData module1Data = bcraPersonalFactorsService.saveModule(bcraPersonalFactors);
        map.put("PersonalFactor", module1Data.getBody());

        //生理因素表填写
        BcraPhysiologicalFactors bcraPhysiologicalFactors = questionnaireInfoVo.getBcraPhysiologicalFactors();
        bcraPhysiologicalFactors.setQuestionnaireCode(questionnaireCode);
        ResultData module2Data = bcraPhysiologicalFactorsService.saveModule(bcraPhysiologicalFactors);
        map.put("PhysiologicalFactors", module2Data.getBody());

        //生活习惯表填写
        BcraLifeHabits bcraLifeHabits=questionnaireInfoVo.getBcraLifeHabits();
        bcraLifeHabits.setQuestionnaireCode(questionnaireCode);
        ResultData module3Data =bcraLifeHabitsService.saveModule(bcraLifeHabits);
        map.put("LifeHabitsFactors", module3Data.getBody());

        //个人健康信息
        BcraHealthInformation bcraHealthInformation=questionnaireInfoVo.getBcraHealthInformation();
        bcraHealthInformation.setQuestionnaireCode(questionnaireCode);
        ResultData module4Data =bcraHealthInformationService.saveModule(bcraHealthInformation);
        map.put("HealthInformation", module4Data.getBody());

        //个人及家族病史
        List<BcraFamilyHistory> bcraFamilyHistoryList = questionnaireInfoVo.getBcraFamilyHistoryList();
        for (BcraFamilyHistory bcraFamilyHistory : bcraFamilyHistoryList) {
            bcraFamilyHistory.setQuestionnaireCode(questionnaireCode);
        }
        ResultData module5Data =bcraFamilyHistoryService.saveModule(bcraFamilyHistoryList);
        map.put("FamilyHistory", module5Data.getBody());

        //基因检测
        List<BcraGeneTest> bcraGeneTestList = questionnaireInfoVo.getBcraGeneTestList();
        for (BcraGeneTest bcraGeneTest : bcraGeneTestList) {
            bcraGeneTest.setQuestionnaireCode(questionnaireCode);
        }
        ResultData module6Data =bcraGeneTestService.saveModule(bcraGeneTestList);
        map.put("GeneTest", module6Data.getBody());

        //创建回收表
        BcraQuestionnaireResponse bcraQuestionnaireResponse =new BcraQuestionnaireResponse();
        bcraQuestionnaireResponse.setQuestionnaireCode(questionnaireCode);
        bcraQuestionnaireResponse.setQuestionnaireName(name);
        bcraQuestionnaireResponse.setProvince(bcraPersonalFactors.getProvince());
        bcraQuestionnaireResponse.setPhone(phone);
        bcraQuestionnaireResponse.setState("To be Submitted");

        bcraQuestionnaireResponseMapper.insert(bcraQuestionnaireResponse);
        map.put("QuestionnaireResponse", bcraQuestionnaireResponse);

        return ResultData.getResult(map);
    }

    /**
     * 提交问卷
     * @param questionnaireCode
     * @return
     */
    @Override
    public ResultData updateState(String questionnaireCode) {
        UpdateWrapper<BcraQuestionnaireResponse> updateWrapper = new UpdateWrapper<>();

        updateWrapper.eq("questionnaire_code",questionnaireCode).set("state","Under Review");

        int update = bcraQuestionnaireResponseMapper.update(null, updateWrapper);
        if (update!=1){
            throw new BaseException(ResponseEnum.STATE_UPDATE_ERROR); //状态修改异常
        }
        return ResultData.getResult(update);
    }

    /**
     * 删除问卷
     * @param questionnaireCode
     * @return
     */
    @Override
    public ResultData deleteQues(String questionnaireCode) {
        List<Object> list =new ArrayList<>();
        ResultData module1Data = bcraPersonalFactorsService.removeByQuesCode(questionnaireCode);
        list.add(module1Data.getBody());

        ResultData module2Data = bcraPhysiologicalFactorsService.removeByQuesCode(questionnaireCode);
        list.add(module2Data.getBody());

        ResultData module3Data = bcraLifeHabitsService.removeByQuesCode(questionnaireCode);
        list.add(module3Data.getBody());

        ResultData module4Data = bcraHealthInformationService.removeByQuesCode(questionnaireCode);
        list.add(module4Data.getBody());

        ResultData module5Data = bcraFamilyHistoryService.removeByQuesCode(questionnaireCode);
        list.add(module5Data.getBody());

        ResultData module6Data = bcraGeneTestService.removeByQuesCode(questionnaireCode);
        list.add(module6Data.getBody());

        return ResultData.getResult(list);
    }

    @Override
    public ResultData selectByQuesCode(String questionnaireCode) {
        BcraPersonalFactors module1 = (BcraPersonalFactors) bcraPersonalFactorsService.selectByQuesCode(questionnaireCode).getBody();
        BcraPhysiologicalFactors module2 = (BcraPhysiologicalFactors) bcraPhysiologicalFactorsService.selectByQuesCode(questionnaireCode).getBody();
        BcraLifeHabits module3 = (BcraLifeHabits) bcraLifeHabitsService.selectByQuesCode(questionnaireCode).getBody();
        BcraHealthInformation module4 = (BcraHealthInformation) bcraHealthInformationService.selectByQuesCode(questionnaireCode).getBody();
        List<BcraFamilyHistory> module5 = (List<BcraFamilyHistory>) bcraFamilyHistoryService.selectByQuesCode(questionnaireCode).getBody();
        List<BcraGeneTest> module6 = (List<BcraGeneTest>) bcraGeneTestService.selectByQuesCode(questionnaireCode).getBody();

        QuestionnaireInfoVo questionnaireInfoVo=new QuestionnaireInfoVo(module1,module2,module3,module4,module5,module6);
        return ResultData.getResult(questionnaireInfoVo);
    }

    /**
     * 根据手机号生成问卷编号
     * @param phone
     * @return
     */
    public String getQuestionnaireCode(String phone){
        return new QuestionnaireCode(phone).getResultCode().toString();
    }
}
